package com.googlecode.flex_solder.util  {
  /**
   * $Id: BaseConversion.as 26 2009-02-17 14:13:44Z fuller.sethm $
   * 
   */

  public class BaseConversion {

    private static const DECIMAL_RADIX:int = 10;

    private static var hexCharList:String = "0123456789ABCDEF";

    public static
      function baseToDecimal(hexString:String, radix:int):int  {
      var value:int = 0;
      var baseMultiplier:int = 1;
      for (var idx:int = hexString.length - 1; idx >= 0; idx--) {
	var hex:String = hexString.charAt(idx);
	var decValue:int = hexCharList.indexOf(hex);

	if (decValue >= 0) {
	  value += (decValue * baseMultiplier);
	}
	baseMultiplier = baseMultiplier * radix;
      }
      return value;
    }

    public static
      function decimalToBase(decimalValue:int, radix:int):String {
      var baseString:String = "";
      var baseRevString:String = "";
      var decimalNum:int = decimalValue;


      for (; decimalNum > 0; decimalNum = decimalNum / radix) {
	var value:int = decimalNum % radix;

	if (value >= DECIMAL_RADIX)
	  {
	    baseRevString += hexCharList.charAt(value);
	  }
	else
	  {
	    baseRevString += String(value);
	  }
      }


      for (var idx:int = baseRevString.length; idx >= 0; idx--) {
	var baseChar:String = baseRevString.charAt(idx);
	baseString += baseChar;
      }

      return baseString;
    }
   
    public function BaseConversion() {

    }

    public function toString():String {
      return "BaseConversion()";
    }
  }
}